#include <stdlib.h>
#include "matrix.h"

#define X(type, mode, f) void apply##type##mode (type **ptr, const unsigned int i, const unsigned int j, void function ()) {\
	unsigned int c1, c2;\
	for (c1 = 0; c1 < i; c1++){\
		for (c2 = 0; c2 < j; c2++)\
			function (&ptr[c1][c2]);\
			f\
	}\
}

GENERATE
#undef X

void **allocate (const unsigned int i, const unsigned int j, const unsigned int size) {
	void **temp;
	register unsigned int c; 
	
	if ((temp = malloc (size * i)) == (void *)0) 
	   goto EXIT; 
	
	for (c = 0; c < i; c++)
		if ((temp[c] = malloc (size * j)) == (void *)0)
		    goto ALLOC_FAIL; 
  
    goto ALLOC_OK; 
    
EXIT: 
    return (void *)0; 
    
ALLOC_FAIL: 
    for (; c; c--) 
        free (temp[c]); 
    return (void *)0; 
    
ALLOC_OK: 		
	return temp;
}

void deallocate (void **ptr, const unsigned int i) {
     register unsigned int c; 
     
     for (c = 0; c < i; c++) 
         free (ptr[c]);
     free (ptr);
}
